<?php
class cartControl extends skymvc{
	public $oc_where;
	public function __construct(){
		parent::__construct();	
	}
	
	public function onInit(){
		$this->oc_where=M("login")->userid?" (userid=".M("login")->userid." or oc_ssid='".OC_SSID."') ":"oc_ssid='".OC_SSID."' ";	
		$this->oc_where.=" AND siteid=".SITEID;	
	}
	
	public function onDefault(){
		//夜猫店
		 
		$this->smarty->goAssign(array(
			"ymcart"=>$this->ymCart(),
			"kdcart"=>$this->kdCart(),
			"shopcart"=>$this->shopcart()
		));
		$tpl=M("pagetpl")->get("cart","index");
		$this->smarty->display($tpl);
		 
		
	}
	
	public function ymCart(){
		$where=$this->oc_where;
		
		$list=M("ymdian_order_cart")->select(array("where"=>$where));
		
		 
		if($list){
			foreach($list as $v){				
				$shopids[]=$v['shopid'];
				$productids[]=$v['productid'];
				$ksids[]=$v['ksid'];
				
			}
			
			if($productids){
				$products=M("product")->id_list(array("where"=>" id in("._implode($productids).") "));
				$kslist=M('product_ks')->id_list(array("where"=>"  id in("._implode($ksids).") "));
				$shoplist=M('ymdian')->id_shop(array("where"=>" id in("._implode($shopids).")"));
			}
			$total_money=0; 
			$total_num=0;
			foreach($list as $k=>$v){
			 	$total_num+=$v['amount'];
				$product=$products[$v['productid']]; 
				$v['title']=$product['title'];
				$v['imgurl']=images_site($product['imgurl']);
				if(isset($kslist[$v['ksid']])){
					$ks=$kslist[$v['ksid']];
					$v['price']=$product['lower_price']>0?$product['lower_price']:$ks['price'];
					$v['ks_title']=$ks['title'];
				}else{
					$v['price']=$product['lower_price']>0?$product['lower_price']:$product['price'];
				}
				$total_money+=$v['price']*$v['amount'];
				$shoplist[$v['shopid']]['products'][]=$v;
			}
			foreach($shoplist as $kk=>$vv){
				$total_money=0; 
				$total_num=0;
				foreach($vv['products'] as $v){
					$total_money+=$v['price']*$v['amount'];
					$total_num+=$v['amount'];
				}
				$shoplist[$kk]['total_money']=$total_money;
				$shoplist[$kk]['total_num']=$total_num;
			}
		 
			return $shoplist; 
		}
		
		return false; 
 
	}
	
	
	public function kdCart(){
		$where=$this->oc_where;
		
		$list=M("koudai_order_cart")->select(array("where"=>$where));
		
		 
		if($list){
			foreach($list as $v){				
				$shopids[]=$v['shopid'];
				$productids[]=$v['productid'];
				$ksids[]=$v['ksid'];
				$ids[]=$v['object_id'];
			}
			
			if($productids){
				$products=M("product")->id_list(array("where"=>" id in("._implode($productids).") "));
				$koudai_products=M('koudai_product')->id_List(array("where"=>"id in("._implode($ids).")"));
				$shoplist=M('koudai')->id_shop(array("where"=>" id in("._implode($shopids).")"));
			}
			$total_money=0; 
			$total_num=0;
			foreach($list as $k=>$v){
			 	$total_num+=$v['amount'];
				$product=$products[$v['productid']]; 
				$v['title']=$product['title'];
				$v['imgurl']=images_site($product['imgurl']);
				$v['price']=$product['lower_price']>0?$product['lower_price']:$koudai_products[$v['object_id']]['price'];
				$total_money+=$v['price']*$v['amount'];
				$shoplist[$v['shopid']]['products'][]=$v;
			}
			 
			foreach($shoplist as $kk=>$vv){
				$total_money=0; 
				$total_num=0;
				foreach($vv['products'] as $v){
					$total_money+=$v['price']*$v['amount'];
					$total_num+=$v['amount'];
				}
				$shoplist[$kk]['total_money']=$total_money;
				$shoplist[$kk]['total_num']=$total_num;
			}
		 
			return $shoplist; 
		}
		
		return false; 
 
	}
	
	
	public function shopCart(){
		$where=$this->oc_where;
		
		$list=M("shop_order_cart")->select(array("where"=>$where));
		
		 
		if($list){
			foreach($list as $v){				
				$shopids[]=$v['shopid'];
				$productids[]=$v['object_id'];
				$ksids[]=$v['ksid'];
				
			}
			
			if($productids){
				$products=M("shop_product")->id_list(array("where"=>" id in("._implode($productids).") "));
				$kslist=M('shop_product_ks')->id_list(array("where"=>"  id in("._implode($ksids).") "));
				$shoplist=M('shop')->id_shop(array("where"=>" shopid in("._implode($shopids).")"));
			}
			 
			$total_money=0; 
			$total_num=0;
			foreach($list as $k=>$v){
			 	$total_num+=$v['amount'];
				$product=$products[$v['object_id']]; 
				$v['title']=$product['title'];
				$v['imgurl']=images_site($product['imgurl']);
				if(isset($kslist[$v['ksid']])){
					$ks=$kslist[$v['ksid']];
					$v['price']=$product['lower_price']>0?$product['lower_price']:$ks['price'];
					$v['ks_title']=$ks['title'];
				}else{
					$v['price']=$product['lower_price']>0?$product['lower_price']:$product['price'];
				}
				$total_money+=$v['price']*$v['amount'];
				$shoplist[$v['shopid']]['products'][]=$v;
			}
			foreach($shoplist as $kk=>$vv){
				$total_money=0; 
				$total_num=0;
				foreach($vv['products'] as $v){
					$total_money+=$v['price']*$v['amount'];
					$total_num+=$v['amount'];
				}
				$shoplist[$kk]['total_money']=$total_money;
				$shoplist[$kk]['total_num']=$total_num;
			}
		 
			return $shoplist; 
		}
		
		return false; 
 
	}
	
}
?>